﻿// LC222_黎锦坪.cpp 

#include <iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftCount = countNodes(root->left);
        int rightCount = countNodes(root->right);
        return 1 + leftCount + rightCount;
    }
};

TreeNode* createSampleTree() {
    TreeNode* node4 = new TreeNode(4);
    TreeNode* node5 = new TreeNode(5);
    TreeNode* node2 = new TreeNode(2, node4, node5);
    TreeNode* node3 = new TreeNode(3);
    TreeNode* root = new TreeNode(1, node2, node3);
    return root;
}

int main()
{
    TreeNode* root = createSampleTree();

    Solution solution;
    int nodeCount = solution.countNodes(root);

    cout << "二叉树的节点个数为: " << nodeCount << endl;
}


